Disk Cleaner - a utility written in Liberty Basic
[Editor: Gordon Sweet produces many useful and sometimes fun Liberty Basic Programs which appear frequently in the newsletter. He submitted this utility for cleaning unwanted files from disk drives. This program requires user intervention to delete specific files, yet it is possible to make mistakes and remove the wrong files.
Care has been take to insure that the program is focusing on what are normally non-critical files, but you still should be familiar with your OS and computer if you are planning to use this program to clean a system. Please read Gordon's explanation before running the program:]
Warning - This program is used to delete files from your computer - use with care!
In Gordon's words he says: First of all I think we should make it very clear that neither I nor anyone else associated with producing these useful Newsletters will be held responsible for the subsequent results of anyone using this program to delete any files on a hard drive. It is possible that this action might cause a fault in the system or setup. Care has been taken to prevent accidental damage by limiting the search to specific file types. Also you will notice in the code below, in the recursive search, that a filter (labeled as [miss]) is included to block certain files from even being listed. Taking a closer look at your PC you would find Windows even omits listing these files using its Search option. Experience has shown that attempting to delete some of these files which Windows hides may well cause this Cleanup program to crash!!
This program works by making use of Mark Parkinson's clever routine to repeatedly search for the type of files some of which MAY POSSIBLY not be needed anymore. The files that are retained (and listed) for your to scrutinize are limited to TMP, CHK, $$$ and ZIP. Once the search is complete a list is created and displayed. The user can then elect to either delete some of the files (by entering number of the file(s) to be deleted) or to quit and search another drive later. Enter the line number of the first and last file to be deleted, or opt for another by another EXT. Obviously to delete only one file, you enter the same number in both the first and last box.
Perhaps it should be explained TMP files are usually intended as Temporary files in the file system. CHK files are generated when some malfunction occurs in an attempt to store or recover lost bytes, which only an expert would be able to restore.
$$$ files and BAK files are also temporary files. As archives go, the user may find some of the ZIP files were not created intentionally by him or her, and may have come with, or have been created by software. Widows XP does have its own option to clean up unnecessary files, but unfortunately someone never had the sense to list all the files, only the categories. We can of course arrange for a Windows Search one by one by .ext which this little program performs all at once.
In order to avoid corrupting the Textbox when the Text window is scrolled, we make sure the file deletion control form is positioned above the file list window, also the window is type popup which effectively prevents the box from being moved.
Please be sure to use the program with care. It is a useful utility, as well as a good learning platform for processes like this. I recommend reading through the program to understand it, as the nature of the program is more risky to your system operation.
Enjoy!
'ONLY WORKS FROM ROOT DIR
nomainwin
dim file$(10000,2) : dim info$(10, 10) : dim Q(2)
open "Background" for graphics_fs_nsb as #bg
#bg "trapclose [quit]; fill 70 70 30; flush"
ux = 1 : uy = 1
if DisplayWidth > 1000 then ux = 120 : uy = 90
[new]
UpperLeftX = ux : UpperLeftY = uy
WindowWidth = 800 : WindowHeight = 580
open "Disk Cleanup" for graphics_nsb as #m
redim file$(10000,2) : redim info$(10, 10) : redim Q(2)
#m "discard; trapclose [quit]; cls; down"
#m "font ariel 30 bold; fill 100 40 10; color white"
#m "backcolor 100 40 10; place 240 80"
#m "\Hard Drive Cleanup"
#m "font ariel 10; color yellow; place 300 110"
#m "\by Gordon Sweet - ver. 1.1 September 2004"
#m "font arial 14 bold; place 120 180"
#m "\This lists all the BAK, CHK, ZIP, and $$ Files on any Drive"
#m "\Normally it should be safe to delete all such files, but you can"
#m "\select which files you wish to delete."
WindowHeight=260 : WindowWidth=150
UpperLeftX = 350+ux : UpperLeftY = 300+uy
dim letters$(25) : index = 0 : DRV$="c:"
while word$(Drives$, index + 1) <> ""
letters$(index) = word$(Drives$, index + 1)
index = index + 1
wend
statictext #choose, "Double-click to pick a drive:", 5, 10, 200, 20
listbox #choose.list, letters$(, [Made], 20, 35, 100, 150
open "Scan drive" for dialog_popup as #choose
print #choose, "trapclose [quitdrv]"
mainH=hWnd(#choose) : gosub [boxhold]
win1=1 : winc=1 : #m "flush" : wait
[Made] print #choose.list, "selection? DRV$"
[quitdrv]
close #choose : winc = 0
DRV$=upper$(DRV$) : tdr$ = "Current Search Drive "+DRV$
close #m : win1 = 0
[find]
Q(1) = 0 : wint = 0
' separate search for each file type
ext$ = "*.bak" : gosub [search]
ext$ = "*.tmp" : gosub [search]
ext$ = "*.chk" : gosub [search]
ext$ = "*.$$$" : gosub [search]
ext$ = "*.zip" : gosub [search]
[newrun]
' store details in temporary file to quickly
' display avoiding any screen flicker
open "c:\temp.$$$" for output as #temp
for h = 1 to Q(1)
#temp h;" = ";file$(h,1)
next h
close #temp
UpperLeftX = 0 : UpperLeftY = 50
WindowWidth = DisplayWidth : WindowHeight = DisplayHeight -50
open "File List - ENTER FILES TO DELETE OR EXT FOR FRESH SEARCH" for text as #t
#t "!trapclose [quit]"
#t "!font arial 8"
wint = 1
open "c:\temp.$$$" for input as #2
#t "!contents #2";
close #2
kill "c:\temp.$$$" ' destroy file now displayed
#t "!lines"
input #t, lpos
#t "!origin 1 1";
[box]
if bx=1 then close #1
' place box top of screen outside Window
' to avoid corruption if using scroll bar.
UpperLeftX = 200+ux : UpperLeftY = 1
WindowWidth = 500: WindowHeight = 80
statictext #1 "First", 30, 1, 30, 12
statictext #1, "Last", 100, 1, 30, 12
statictext #1, "EXT to find", 270, 1, 70, 12
textbox #1.tbox1, 20, 15, 40, 20
textbox #1.tbox2, 90, 15, 40, 20
textbox #1.tbox3, 280, 15, 40, 20
button #1.default, "LIST DELETIONS", [pass], UL, 150, 12
button #1.default, "SEARCH", [pass], UL, 340, 12
button #1, "QUIT", [quit], UL, 440, 10
open " " for dialog_popup as #1
print #1, "trapclose [npass]" : bx=1
print #1.tbox1, "!setfocus"
[npass]
mainH=hWnd(#1) : gosub [boxhold]
wait
[pass]
print #1.tbox1, "!contents? A$" : first = val(A$)
print #1.tbox2, "!contents? B$" : last = val(B$)
print #1.tbox3, "!contents? C$" : search$ = C$
if C$ <> "" then [newsearch]
if first < 1 or last > Q(1) or last < first then [inval]
close #1 : close #t
open "test" for text_fs as #t
#t "!trapclose [quit]"
#t "font fixedsys"
for d = first to last
#t file$(d,1)
next
UpperLeftX = 200 : UpperLeftY = 40
WindowWidth = 280 : WindowHeight = 60
button #1, "DELETE FILES ", [del], UL 10, 5
button #1, "DO NOT DELETE", [no], UL 150, 5
open "OK TO DELETE THESE FILES " for dialog as #1
print #1, "trapclose [QUIT]"
playwave "ooo" : wait
close #1 : bx = 0
[del]
for d = first to last
kill file$(d,1)
next
[no]
close #t : wint = 0
goto [find]
[newsearch]
close #1 : bx = 0
close #t : wint = 0
notice "** WARNING ** Beware of deleting other files !!"
Q(1) = 0 : ext$ = "*." + search$
gosub [search]
goto [newrun]
[inval]
notice "INVALID ENTRY !!"
goto [npass]
[quit]
if winc = 1 then close #choose
if win1 = 1 then close #m
if wint = 1 then close #t
if bx = 1 then close #1
close #bg
end
[search]
sf$ = right$(ext$,3)
#bg "fill 70 70 30; backcolor 70 70 30; color white"
#bg "font ariel 14 bold; color white; place ";200+ux;" 270"
#bg "\PLEASE WAIT FILE LIST BEING CREATED"
#bg "\OF ALL OF THE ";upper$(sf$);" FILES IN DRIVE ";DRV$
' recurse files.bas for LB3
' Mark Parkinson
' 08/10/02
' This program will list all the files in the whole directory structure
' which match the given file spec. A process called recursion is used.
' Feel free to use and adapt the program eg to sum the filespace used or
' delete files or rename files or move them.
' It is also easy to adapt the program to delete empty subdirectories.
placetohunt$=DRV$ : thingtohuntfor$=ext$ : sf$=right$(ext$,3)
call recurse placetohunt$, thingtohuntfor$
for f = 1 to Q(1)
tfile$=file$(f,1)
t$="" : ltr = len(tfile$)
while t$<>"\"
t$ = mid$(tfile$,ltr,1)
ltr = ltr -1
wend
n = len(tfile$)-ltr-1
file$(f,2) = right$(tfile$,n)
if len(file$(f,2))>18 then file$(f,2)=left$(file$(f,2),18)
next f
'playwave "chord"
return
sub recurse pathspec$,mask$
'Put in the backslash separator.
pathspec$=pathspec$+"\"
files pathspec$, mask$, info$(
filecount=val(info$(0, 0))
subdircount=val(info$(0, 1))
for i=1 to filecount
filename$= pathspec$+info$(i, 0)
filesize$= info$(i, 1)
datestamp$=info$(i, 2)
' THESE DO NOT SHOW UP WITH A WINDOWS SEARCH
if instr(filename$,"Local Settings")>0 then [miss]
if instr(filename$,"Application Data")>0 then [miss]
if instr(filename$,"WINDOWS\system")>0 then [miss]
Q(1) = Q(1) + 1
file$(Q(1),1) = upper$(filename$)
[miss] next i
for i=1 to subdircount
list$=list$+pathspec$+info$(f + i, 1)+"*"
next i
while list$<>""
p=instr(list$,"*")
p$=left$(list$,p-1)
call recurse p$,mask$
list$=mid$(list$,p+1)
wend
end sub
[boxhold]
open "user32" for dll as #user ' prevents losing box
toTop=(-1 or 0)
flags=_SWP_NOMOVE or _SWP_NOSIZE
calldll #user,"SetWindowPos",mainH as ushort,toTop as short,_
0 as short,0 as short,0 as short,0 as short,flags as ushort,_
result as void
close #user : return